<?php

/**
 * 妈咪秘籍
 */
class CheatsController extends AdminBase{
    public function actionIndex() {
        $this->baseIndex('CheatsModel');
        $this->render();
    }

    public function actionAdd() {
        $model = new CheatsModel('admin');
        $this->assign('data', $model);
        if (IS_POST) {
            $model->attributes = $_POST['CheatsModel'];
            if ($model->save()) {

                //是否有上传图像
                if (UploadFile::getInstance()->upload()){
                    $d = UploadFile::getInstance()->getUploadFileInfo();
                    foreach($d as $value){
                        $model->$value['key'] = $value['savepath'] . $value['savename'];
                    }
                    $model->save();
                }

                $this->success('添加成功！', self::U('index'));
            } else {
                $error = $model->getOneError();
                $this->error($error ? $error : '添加失败！');
            }
        } else {
            $this->render();
        }
    }

    public function actionEdit() {

        $model = CheatsModel::model();
        $id = Yii::app()->request->getParam($model->getTableSchema()->primaryKey);
        $data = $model->findByPk($id);
        if (empty($data)) {
            $this->error('该信息不存在！');
        }
        if (IS_POST) {
            $data->attributes = $_POST['CheatsModel'];
            if ($data->save()) {

                //是否有上传图像
                if (UploadFile::getInstance()->upload()){
                    $d = UploadFile::getInstance()->getUploadFileInfo();
                    foreach($d as $value){
                        //如果存在这文件了则删除这附件。
                        if(!empty($data->$value['key'])){
                            $m = AttachmentModel::model()->find(array('condition' => 'filepath = :filepath', 'params' => array('filepath' => $data->$value['key'])));
                            if($m){
                                $m->deleteFileByAid($m->aid);
                            }
                        }
                        $data->$value['key'] = $value['savepath'] . $value['savename'];
                    }
                    $data->save(false);
                }

                $this->success('修改成功！', self::U($url));
            } else {
                $error = $data->getOneError();
                $this->error($error ? $error : '修改失败！');
            }
        } else {
            $this->assign('data', $data);
            $this->render();
        }

    }

    public function actionDelete() {
        $model = $this->getModelObject('CheatsModel');
        $id = Yii::app()->request->getParam($model->getTableSchema()->primaryKey);
        //查询信息
        $data = $model->findByPk($id);
        $old_data = $data;
        if (empty($data)) {
            $this->error('该信息不存在！');
        }
        if ($data->delete()) {

            if(!empty($old_data->img1)){
                $m = AttachmentModel::model()->find(array('condition' => 'filepath = :filepath', 'params' => array('filepath' => $old_data->img1)));
                if($m){
                    $m->deleteFileByAid($m->aid);
                }
            } 
            if(!empty($old_data->img2)){
                $m = AttachmentModel::model()->find(array('condition' => 'filepath = :filepath', 'params' => array('filepath' => $old_data->img2)));
                if($m){
                    $m->deleteFileByAid($m->aid);
                }
            } 
            if(!empty($old_data->img3)){
                $m = AttachmentModel::model()->find(array('condition' => 'filepath = :filepath', 'params' => array('filepath' => $old_data->img3)));
                if($m){
                    $m->deleteFileByAid($m->aid);
                }
            } 


            $this->success('删除成功！', self::U('index'));
        } else {
            $this->error('删除失败！');
        }        
    }	
}